package lock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.LongAdder;

/**
 * @Classname PrintByAtomicInteger
 * @Description TODO
 * @Date 2020/2/16 1:27 下午
 * @Created by gule
 */
public class PrintByAtomicInteger {

    static private LongAdder count = new LongAdder();
    private static Integer defaultValue = 1000;
    private volatile static boolean flag = true;

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newCachedThreadPool();

        executorService.execute(()->{
            while (count.intValue() < defaultValue){
                if(flag) {
                    System.out.println("偶数 num is " + count);
                    count.increment();
                    flag = false;
                }
            }
        });
        executorService.execute(()->{
            while (count.intValue() < defaultValue){
                if(!flag) {
                    System.out.println("奇数 num is " + count);
                    count.increment();
                    flag = true;
                }
            }
        });

    }
}
